package com.pentajock.docdb.lang;

/*
 * Liscense: 	GPL http://www.gnu.org/copyleft/gpl.html
 * By: 		A. Tres Finocchiaro
 * Date:	2007
 * Java:	6.0SE
 */

/* 
 * Lang.java
 *
 * 	Loads language information 
 * 	from a Language file (i.e. .lang/default.ini)
 *
 */


import com.pentajock.docdb.Main;
import com.pentajock.docdb.dialogs.Dialogs;
import com.pentajock.docdb.utils.QuickIni;
import com.pentajock.docdb.utils.Utils;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Locale;

public enum Lang {
	ADMIN_DIALOG	("ADMIN_DIALOG"),
	STATUS_BAR	("STATUS_BAR"),
	TOOL_TIP 	("TOOL_TIP"),
	LISTING		("LISTING"),
	FILE_IO		("FILE_IO"),
	BUTTON		("BUTTON"),
	DIALOG		("DIALOG"),
	HELP		("HELP"),
	MISC		("MISC"),
	MENU		("MENU");
	
	private String appComponent;
	private String delimiter = "<U>";
	private static String language;
	
	private static QuickIni langIni = null;
	
	
	/*
	 * Constructor - Sets the enumerated value 
	 * above which references that area of the ini File 
	 */
	Lang(String appComponent) {
		this.appComponent = appComponent;
	}
	
	/* 
	 * Sets the language based on the name of the file
	 *  If the file doesn't exist, it attempts to copy it
	 *  from the jar
	 */
	public static boolean setLanguage(String language) {
		Lang.language = language;
		String source = "/com/pentajock/docdb/lang/" + language;
		String destination = System.getProperty("user.dir") + "/lang/" + language; //+ folder;
		File langFile = new File(destination);
		if (!langFile.exists()) {
			Utils.copyFromJar(source, destination);
		}
		langIni = new QuickIni(langFile.getPath());
		if (getLanguageName() == null) {
			if (language.equals("default.ini")) {
				Main.printDebug("No language file specified");
				return false;
			}
			Dialogs.showError(langFile, "Error loading language file " + 
				"ensure it is in the \"lang/\" folder. (Program will continue.)");
			return false;
		}
		else {
			Main.printDebug("Successfully loaded \"" + langIni.getStringProperty("MISC", "THIS_LANGUAGE") + "\" language file");
		}
		Locale.setDefault(new Locale(Lang.MISC.getText("LOCALE", "us").toLowerCase()));
		return true;
	}

	public static String getLanguage() {
		return language;
	}
	
	public static String getLanguageName() {
		return langIni.getStringProperty("MISC", "THIS_LANGUAGE");
	}
	
	public static String getLanguageName(File langFile) {
		if (langFile.getName().equalsIgnoreCase("default.ini"))
			return Lang.MISC.getText("DEFAULT", "Default");
		String name = new QuickIni(langFile.getPath()).getStringProperty("MISC", "THIS_LANGUAGE");
		return name == null ? langFile.getName() : name;
	}

	/* 
	 * Return the language value if its not null.
	 * If it is, return a default inputed value 
	 */
	public String get(String val, String defaultVal) {
		String temp = get(val);
		if (temp == null)
			Main.printDebug("Error loading language value: [" + appComponent + "]: " + val + " for " + language);
		return (temp == null ? defaultVal : temp);
	}
	
	/* 
	 * Return the mnemonic value if its not VK_UNDEFINED.
	 * If it is, return a default inputed value 
	 */
	public int getMnemonic(String val, int defaultVal) {
		try {
			String langValue = get(val);
			int index = langValue.indexOf(delimiter) + delimiter.length();
			// Char >> String >> Uppercase >> Char >> Int
			int virtualKey = (int)String.valueOf(langValue.charAt(index)).toUpperCase().charAt(0);
			//Main.printDebug("VK = " + virtualKey + "(" + String.valueOf((char)virtualKey) + ")");
			return virtualKey;
		}
		catch (Exception e) {
			//Main.printDebug("Error retreiving Mnemonic character for " + cat + "." + val);
			return defaultVal;
		}
	}
	
	public char getDisplayedMnemonic(String val, char defaultVal) {
		return (char)getMnemonic(val, (int)defaultVal);
	}
	
	/* 
	 * Return the mnemonic value, or VK_UNDEFINED 
	 * if one can't be found 
	 */
	public int getMnemonic(String val) {
		return getMnemonic(val, KeyEvent.VK_UNDEFINED);
	}
	
	/* Return the language value */
	public String get(String val) {
		if (langIni == null || val == null || val == "") return null;
		try {
			String temp = langIni.getStringProperty(appComponent, val);
			return (temp.trim() == "" ? null : "<HTML>" + temp + "</HTML>");
		}
		catch (Exception e) {
			return null;
		}
	}
	
	/* 
	 * Return language value and strip HTML 
	 */
	public String getText(String val) {
		return get(val).replaceAll("\\<.*?\\>","");
	}

	
	/* 
	 * Return the language value and strip HTML if its not null.  
	 * If it is, return a default inputed value 
	 */
	public String getText(String val, String defaultVal) {
		String temp = get(val);
		return (temp == null ? defaultVal : temp.replaceAll("\\<.*?\\>",""));
	}
	
	/*
	 * Return a parsed integer value in the language file
	 */
	 public int getInt(String val) {
		 try {
			 return Integer.parseInt(getText(val));
		 }
		 catch (NumberFormatException nfe) {
			 return -1;
		 }
	 }
	 
	 
	 /*
	 * Return a parsed integer value in the language file
	 */
	 public int getInt(String val, int defaultVal) {
		 try {
			 return Integer.parseInt(getText(val));
		 }
		 catch (NumberFormatException nfe) {
			 return defaultVal;
		 }
	 }
}
